package hip.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;


public class StrUtil {
    public static final String allUpperChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String toStr(Object o) {
        return o == null ? "" : o.toString();
    }

    public static String toTrimStr(Object o) {
        return o == null ? "" : o.toString().trim();
    }

    public static String generateStr(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        int fLen = 8;//头长度
        int lLen = 12;//尾长度
        for (int i = 0; i < length; i++) {
            if (i == fLen) {
                sb.append("-");
                if (length - fLen - 1 > lLen) {
                    fLen = fLen + 5;
                }
            } else {
                sb.append(allUpperChar.charAt(random.nextInt(allUpperChar.length())));
            }
        }
        return sb.toString();
    }

    public static String getEncoding(String str) {
        String encode[] = new String[]{"UTF-8", "GBK", "ISO-8859-1", "GB2312", "Unicode", "ASCII"};
        for (int i = 0; i < encode.length; i++) {
            try {
                if (str.equals(new String(str.getBytes(encode[i]), encode[i]))) {
                    return encode[i];
                }
            } catch (Exception ex) {
            }
        }

        return "";
    }

    public static byte[] getUTF8BytesFromGBKStr(String gbkStr) {
        int n = gbkStr.length();
        byte[] utfBytes = new byte[3 * n];
        int k = 0;
        for (int i = 0; i < n; i++) {
            int m = gbkStr.charAt(i);
            if (m < 128 && m >= 0) {
                utfBytes[k++] = (byte) m;
                continue;
            }
            utfBytes[k++] = (byte) (0xe0 | (m >> 12));
            utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
            utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
        }
        if (k < utfBytes.length) {
            byte[] tmp = new byte[k];
            System.arraycopy(utfBytes, 0, tmp, 0, k);
            return tmp;
        }
        return utfBytes;
    }

    public static String multiReplace(String source, Object[] arr) {
        for (int i = 0; i < arr.length; i++) {
            String regex = "{" + i + "}";
            source = source.replace(regex, arr[i].toString());
        }
        return source;
    }

    // 通过EMPI生成分区标识;
    public static synchronized String getLastChar(String mpiID) {
        if (StringUtils.isNotBlank(mpiID)) {
            int length = mpiID.length();
            return mpiID.substring(length - 1, length).toLowerCase();
        }
        return StringUtils.EMPTY;
    }

    public static synchronized String replaceChar(String value) {
        if (value != null) {
            value.replaceAll("\"", "&quot;");//替换双引号
            value.replaceAll("\'", "&acute;");//替换单引号
            value.replaceAll("<", "&lt;");//替换<
            value.replaceAll(">", "&gt;");//替换>
            value.replaceAll("&", "&amp;");//替换&
        }
        return value;
    }

    public static String removeQuota(String str) {
        if (str != null) {
            String str1 = str.replaceAll("\"", "");
            return str1.replaceAll("\'", "");
        }
        return null;
    }

}
